package com.mxl.springsecurity.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * @author MXL
 * @date 2022/3/9
 **/


@Component
@Slf4j
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 配置密码不需要加密
     * @return
     */
    @Bean
    public PasswordEncoder passWordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //ignoring()配置要忽略验证的url
        web.ignoring().antMatchers("/js/**");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //授权请求
        http.authorizeRequests()
                //任何请求 都要 认证
                .anyRequest().authenticated()
                .and()
                //表单登录
                .formLogin()
                //处理登录的url
                .loginProcessingUrl("/login").successHandler(new AuthenticationSuccessHandler() {
                    @Override
                    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
                        log.info("登录成功!");
                        PrintWriter out = response.getWriter();
                        out.println("登录成功!");
                        response.setContentType("application/json; charset=UTF-8");
                        out.close();
                    }
                })
                .failureHandler(new AuthenticationFailureHandler() {
                    @Override
                    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
                        log.error("登录失败!");
                        PrintWriter out = response.getWriter();
                        out.println("登录失败!");
                        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
                        out.close();
                    }
                })
                //表示登录相关的页面或者接口不要被拦截
                .permitAll()
                .and()
                .csrf()
                //禁止csrf攻击
                .disable();
    }
}
